/*
 * Date          Author     Note
 * 2015.04.21    舞者无罪          版本维护
 * 2015.06.24    wzwz       增加返回按钮
 */
#include <stdio.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "maint.h"
#include "com.h"
#include "version.h"
#include "ver.h"
#include "payment.h"

#define VERSION_CAPTION_LEFT    (30)
#define VERSION_CAPTION_TOP     (60)
#define VERSION_CAPTION_WIDTH   (200)
#define VERSION_CAPTION_HEIGHT  (30)
/*
 *
 */
static void CreateVersionInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_OTHER_INPUT1_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_OTHER_PARAM,
        VERSION_CAPTION_LEFT, VERSION_CAPTION_TOP, VERSION_CAPTION_WIDTH, VERSION_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_MAC_CONF2_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_MAC,
        VERSION_CAPTION_LEFT, VERSION_CAPTION_TOP + VERSION_CAPTION_HEIGHT,
        VERSION_CAPTION_WIDTH, VERSION_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_VERSION3_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_VERSION,
        VERSION_CAPTION_LEFT, VERSION_CAPTION_TOP + VERSION_CAPTION_HEIGHT * 2,
        VERSION_CAPTION_WIDTH, VERSION_CAPTION_HEIGHT,
        hWnd, 0);
    char esc[64] = {0};
    sprintf(esc, GetString(802), 4);
    CreateWindow(MH_BTN_NAME,
        esc,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_ESC,
        30,
        60 + 30 * 3,
        200,
        30,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_OTHER_PARAM:
        {
            CreatePaymentWindow(hWnd);
        }
        break;
    case ID_VERSION:
        {
            CreateVerWindow(hWnd);
        }
        break;
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    default:
        break;
    }
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    switch(nID)
    {
    case ID_OTHER_PARAM:
        if(up)
        {
            //
        }
        else
        {
            nID = ID_BTN_MAC;
        }
        break;
    case ID_BTN_MAC:
        if(up)
        {
            nID = ID_OTHER_PARAM;
        }
        else
        {
            nID = ID_VERSION;
        }
        break;
    case ID_VERSION:
        if(up)
        {
            nID = ID_BTN_MAC;
        }
        else
        {
            nID = ID_ESC;
        }
        break;
    case ID_ESC:
        if(up)
        {
            nID = ID_VERSION;
        }
        break;
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_4)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_OTHER_PARAM));
        OnBtnCmd(hWnd, ID_OTHER_PARAM);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_VERSION));
        OnBtnCmd(hWnd, ID_VERSION);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int VersionWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateVersionInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateVersionWindow(HWND hHosting)
{
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_VERSION_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = VersionWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_OTHER_PARAM));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
